package com.example.fengyungo.controller;

import com.example.fengyungo.constant.MsgConstant;
import com.example.fengyungo.constant.OrderDriverConstant;
import com.example.fengyungo.domain.pojo.OrderDriver;
import com.example.fengyungo.result.Result;
import com.example.fengyungo.service.DriverService;
import com.example.fengyungo.service.OrderDriverService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/orderDriver")
public class OrderDriverController {

    @Autowired
    private OrderDriverService orderDriverService;

    @Autowired
    private DriverService driverService;

    /**
     * 司机愿意接受订单
     *
     * @param orderDriver OrderDriver
     * @return Result
     */
    @PostMapping
    public Result addOrderDriver(@RequestBody OrderDriver orderDriver) {
        // 检查司机状态是否具备接单权限
        Boolean judgeStatus = driverService.judgeStatus(orderDriver.getDriverId());
        if (!judgeStatus) {
            return Result.error(MsgConstant.NOT_DRIVER);
        }
        Integer res = orderDriverService.addOrderDriver(orderDriver);
        if (res < 0) {
            return Result.error(OrderDriverConstant.ACCEPT_ORDER_FAIL);
        }
        return Result.success();
    }

    /**
     * 查询愿意接收订单的司机
     *
     * @param number 订单编号
     * @return List<OrderDriver>
     */
    @GetMapping("/{number}")
    public Result<List<OrderDriver>> getOrderDriverByNumber(@PathVariable("number") String number) {
        List<OrderDriver> orderDriverList = orderDriverService.getOrderDriverByNumber(number);
        return Result.success(orderDriverList);
    }

    /**
     * 取消接受订单
     *
     * @param orderDriver OrderDriver
     * @return Result
     */
    @PutMapping
    public Result cancelAcceptOrder(@RequestBody OrderDriver orderDriver) {
        Integer res = orderDriverService.cancelAcceptOrder(orderDriver);
        if (res <= 0) {
            return Result.error(OrderDriverConstant.CANCEL_ORDER_FAIL);
        }
        return Result.success();
    }
}
